package JUC;

//单例模式
public class SingleInstance {

    public static void main(String[] args) {
        Hungry instance = Hungry.getInstance();
        Hungry instance1 = Hungry.getInstance();
        System.out.println(instance==instance1);

        LazyMan lazyMan = LazyMan.getInstance();
        LazyMan lazyMan1 = LazyMan.getInstance();
        System.out.println(lazyMan==lazyMan1);
    }

}


//饿汉式
class Hungry {

    Hungry() {
    }

    ;

    private final static Hungry hungry = new Hungry();

    public static Hungry getInstance() {
        return hungry;
    }

}

//懒汉式
class LazyMan {
    LazyMan() {
    };

    private volatile static LazyMan lazyMan;

    public static LazyMan getInstance() {
        if (lazyMan == null) {
            synchronized (LazyMan.class) {
                if (lazyMan == null) {
                    lazyMan = new LazyMan();
                }
            }
        }
        return lazyMan;
    }
}